import store from '@/store';
import { MessageBox, Message } from 'element-ui';
import router from '@/router';

const customerHTML = `<div style="text-align: center;">
<p>扫描二维码联系客服~</p>
<img style="width:250px;height:250px" src="${require('@/assets/images/qr.jpg')}" />
</div>`;

const strategy = {
  // 正常
  '0': () => {
    return true;
  },
  // 禁用
  '1': () => {
    handleVipError({ content: '您的会员已被禁用，请联系客服解禁~' });
    return false;
  },
  // 过期
  '2': () => {
    handleVipError({ content: '您的会员已过期，请联系客服充值~' });
    return false;
  },
  // 非会员
  '3': () => {
    handleVipError({ content: '该功能需要更高版本会员，请联系客服开通~' });
    return false;
  }
};

function handleVipError({ content }) {
  MessageBox.confirm(content, '系统提示', {
    confirmButtonText: '联系客服',
    cancelButtonText: '我知道了',
    type: 'warning'
  })
    .then(() => {
      MessageBox.confirm(customerHTML, '系统提示', {
        dangerouslyUseHTMLString: true,
        center: true,
        confirmButtonText: '去了解会员',
        showCancelButton: false
      })
        .then(() => {
          router.push({ name: 'vipDetails' });
        })
        .catch(() => {});
    })
    .catch(() => {});
}

const checkPermission = async () => {
  try {
    const res = await store.dispatch('loadUserVip');
    return res && strategy[res] && strategy[res]();
  } catch (error) {
    return false;
  }
};

const checkPermissionByStatus = (status) => {
  return (status || false) && (strategy[status] || false) && strategy[status]();
};

export { checkPermission, checkPermissionByStatus };
